<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="仓库">
              <j-dict-select-tag
                placeholder="请选择仓库"
                v-model="queryParam.warehouseCode"
                dictCode="wms_warehouse,warehouse_name,warehouse_code"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料名称">
              <a-input placeholder="请输入物料名称" v-model="queryParam.materialName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料编码">
              <a-input placeholder="请输入物料编码" v-model="queryParam.materialCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="单据类型">
              <j-dict-select-tag placeholder="请选择单据类型" v-model="queryParam.entryType" dictCode="rk_type" />
            </a-form-item>
          </a-col>
          <a-col :xl="12" :lg="14" :md="12" :sm="24">
            <a-form-item label="入库日期">
              <j-date
                :show-time="true"
                date-format="YYYY-MM-DD"
                placeholder="请选择开始时间"
                class="query-group-cust"
                v-model="queryParam.entryDateBegin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="true"
                date-format="YYYY-MM-DD"
                placeholder="请选择结束时间"
                class="query-group-cust"
                v-model="queryParam.entryDateEnd"></j-date>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="单据号">
                <a-input placeholder="请输入单据号" v-model="queryParam.documentNumber"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="客户物料编码">
                <a-input placeholder="请输入客户物料编码" v-model="queryParam.customerMaterialCode"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="规格型号">
                <a-input placeholder="请输入规格型号" v-model="queryParam.specificationModel"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="宝洁订单号">
                <a-input placeholder="请输入宝洁订单号" v-model="queryParam.pgOrderNumber"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="订单编号">
                <a-input placeholder="请输入订单编号" v-model="queryParam.orderNumber"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="生产车间">
                <j-dict-select-tag placeholder="请选择生产车间" v-model="queryParam.productionShop" dictCode="erp_production_execution_organization,depart_name,depart_code" />
              </a-form-item>
            </a-col>
            <a-col :xl="12" :lg="14" :md="12" :sm="24">
              <a-form-item label="生产日期">
                <j-date :show-time="false" date-format="YYYY-MM-DD" placeholder="开始日期" class="query-group-cust" v-model="queryParam.productionDateBegin"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date :show-time="false" date-format="YYYY-MM-DD" placeholder="结束日期" class="query-group-cust" v-model="queryParam.productionDateEnd"></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="保质期（天）">
                <a-input placeholder="请输入保质期（天）" v-model="queryParam.shelfLife"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="批次号">
                <a-input placeholder="请输入批次号" v-model="queryParam.batchNumber"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="物料批号">
                <a-input placeholder="请输入物料批号" v-model="queryParam.materialBatchNumber"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="储位">
                <a-input placeholder="请输入储位" v-model="queryParam.storageLocation"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="质检状态">
                <j-dict-select-tag placeholder="请选择质检状态" v-model="queryParam.qualityInspectionStatus" dictCode="rk_zj_status" />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="供应商名称">
                <a-input placeholder="请输入供应商名称" v-model="queryParam.supplierName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="宝洁采购订单号">
                <a-input placeholder="请输入宝洁采购订单号" v-model="queryParam.pgPurchaseOrderNumber"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="宝洁供应商编号">
                <a-input placeholder="请输入宝洁供应商编号" v-model="queryParam.pgSupplierCode"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="宝洁供应商">
                <a-input placeholder="请输入宝洁供应商" v-model="queryParam.pgSupplierName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="客户名称">
                <a-input placeholder="请输入客户名称" v-model="queryParam.customerName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="物料分类1">
                <j-dict-select-tag placeholder="请选择物料分类1" v-model="queryParam.materialClassify" dictCode="material_classification_fir" />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="物料分类2">
                <j-dict-select-tag placeholder="请选择物料分类2" v-model="queryParam.brand" dictCode="material_classification_sen" />
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!--<a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>-->
      <a-button type="primary" icon="download" @click="handleExportXls('仓库库存表')">导出</a-button>
      <a-button
        v-if="['W06', 'W07', 'W08'].includes(queryParam.warehouseCode)"
        type="primary"
        @click="handlePackagingWorkRecord"
        >包装作业记录
      </a-button>
      <a-button type="primary" @click="handleHoldRecord">扣货记录</a-button>
      <a-button type="primary" @click="handleMoveRecord">储位调整记录</a-button>
      <a-button type="primary" @click="handleTransferRecord">调拨记录</a-button>
      <a-button type="primary" icon="printer" @click="handlePrint">标签打印</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>

        <span style="float:right;">
          <a-popover title="自定义列" trigger="click" placement="leftBottom">
            <template slot="content">
              <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                <a-row style="width: 400px">
                  <template v-for="(item,index) in defColumns">
                    <template v-if="item.key!=='rowIndex'&& item.dataIndex!=='action'">
                        <a-col :span="12"><a-checkbox :value="item.dataIndex"><j-ellipsis :value="item.title" :length="10"></j-ellipsis></a-checkbox></a-col>
                    </template>
                  </template>
                </a-row>
              </a-checkbox-group>
            </template>
            <a><a-icon type="setting" />设置</a >
          </a-popover>
        </span>

      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="false"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap1"
        @change="handleTableChange"
      >
        <template slot="remark" slot-scope="text, record">
          <div v-if="record.creatorRemark">制单人:{{ record.creatorRemark }}</div>
          <div v-if="record.handlerRemark">经办人:{{ record.handlerRemark }}</div>
          <div v-if="record.inspectorRemark">检验人:{{ record.inspectorRemark }}</div>
        </template>
        <span slot="action" slot-scope="text, record" v-if="record.id != '合计'">
          <a @click="handlePackagingWork(record)" v-if="['W06', 'W07', 'W08'].includes(record.warehouseCode)"
            >包装作业</a
          >
          <a-divider type="vertical" v-if="['W06', 'W07', 'W08'].includes(record.warehouseCode)" />
          <a @click="handleHold(record)">扣货</a>
          <a-divider type="vertical" />
          <a @click="handleMove(record)">储位调整</a>
          <a-divider type="vertical" />
          <a @click="handleTransfer(record)">调拨</a>
        </span>
      </a-table>
      <!-- 页面使用分页组件 -->
      <Pagination
        v-model="ipagination.current"
        :total="ipagination.total"
        show-size-changer
        :page-size="ipagination.pageSize"
        @onShowSizeChange="onShowSizeChange"
        :pageSizeOptions="ipagination.pageSizeOptions"
      ></Pagination>
    </div>

    <packaging-work-modal ref="packagingWorkModal" @ok="modalFormOk" />
    <packaging-work-record-modal ref="packagingWorkRecordModal" @ok="modalFormOk" />
    <hold-record-modal ref="holdRecordModal" @ok="modalFormOk" />
    <storage-location-adjustment-record-modal ref="storageLocationAdjustmentRecordModal" @ok="modalFormOk" />
    <transfer-record-modal ref="transferRecordModal" @ok="modalFormOk" />
    <inbound-record-label-print-modal ref="labelPrintModalForm" />
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
// import InventoryModal from './modules/InventoryModal'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import StorageLocationAdjustmentRecordModal from './modules/StorageLocationAdjustmentRecordModal.vue'
import TransferRecordModal from '@views/wms/inventory/modules/TransferRecordModal.vue'
import InboundRecordLabelPrintModal from '@views/wms/inbound-order/modules/InboundRecordLabelPrintModal.vue'
import HoldRecordModal from '@views/wms/inventory/modules/HoldRecordModal.vue'
import PackagingWorkModal from './modules/PackagingWorkModal.vue'
import PackagingWorkRecordModal from './modules/PackagingWorkRecordModal.vue'
import { getAction} from '@api/manage'
import Pagination from './Pagination.vue'
import utils from '@/utils/utils'
import { countBy } from 'xe-utils/methods'

export default {
  name: 'InventoryList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    PackagingWorkRecordModal,
    PackagingWorkModal,
    InboundRecordLabelPrintModal,
    HoldRecordModal,
    TransferRecordModal,
    StorageLocationAdjustmentRecordModal,
    Pagination,
  },
  data() {
    return {
      isOpenSettingColumns:true,
      description: '仓库库存表管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: 'rowIndex',
          key: 'rowIndex',
          width: 40,
          align: 'center',
          fixed: 'left',
          customRender: function (text, r, index) {
            return r.id !== '合计' ? parseInt(index) + 1 : r.id
          },
        },
        {
          title: '单据类型',
          align: 'center',
          dataIndex: 'entryType_dictText',
          fixed: 'left',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.entryType_dictText || '';
            const valB = b.entryType_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '单据号',
          align: 'center',
          dataIndex: 'documentNumber',
          fixed: 'left',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.documentNumber || '';
            const valB = b.documentNumber || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'materialCode',
          fixed: 'left',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.materialCode || '';
            const valB = b.materialCode || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName',
          fixed: 'left',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.materialName || '';
            const valB = b.materialName || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '客户物料编码',
          align: 'center',
          dataIndex: 'customerMaterialCode',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.customerMaterialCode || '';
            const valB = b.customerMaterialCode || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'specificationModel',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.specificationModel || '';
            const valB = b.specificationModel || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.unit_dictText || '';
            const valB = b.unit_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'quantity',
          sorter: (a, b, order) => {
            const valA = a.quantity;
            const valB = b.quantity;
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            if (valA == null && valB == null) return 0;
            if (valA == null) return 1;
            if (valB == null) return -1;
            return Number(valA) - Number(valB);
          },
        },
        {
          title: '辅单位',
          align: 'center',
          dataIndex: 'secondaryUnit_dictText',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.secondaryUnit_dictText || '';
            const valB = b.secondaryUnit_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '辅数量',
          align: 'center',
          dataIndex: 'secondaryQuantity',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.secondaryQuantity;
            const valB = b.secondaryQuantity;
            if (valA == null && valB == null) return 0;
            if (valA == null) return 1;
            if (valB == null) return -1;
            return Number(valA) - Number(valB);
          },
        },
        {
          title: '宝洁订单号',
          align: 'center',
          dataIndex: 'pgOrderNumber',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.pgOrderNumber || '';
            const valB = b.pgOrderNumber || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '订单编号',
          align: 'center',
          dataIndex: 'orderNumber',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.orderNumber || '';
            const valB = b.orderNumber || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '生产车间',
          align: 'center',
          dataIndex: 'productionShop_dictText',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.productionShop_dictText || '';
            const valB = b.productionShop_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '生产日期',
          align: 'center',
          dataIndex: 'productionDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.productionDate || '';
            const valB = b.productionDate || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '保质期（天）',
          align: 'center',
          dataIndex: 'shelfLife',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.shelfLife;
            const valB = b.shelfLife;
            if (valA == null && valB == null) return 0;
            if (valA == null) return 1;
            if (valB == null) return -1;
            return Number(valA) - Number(valB);
          },
        },
        {
          title: '限用日期',
          align: 'center',
          dataIndex: 'expirationDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.expirationDate || '';
            const valB = b.expirationDate || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '批次号',
          align: 'center',
          dataIndex: 'batchNumber',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.batchNumber || '';
            const valB = b.batchNumber || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '物料批号',
          align: 'center',
          dataIndex: 'materialBatchNumber',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.materialBatchNumber || '';
            const valB = b.materialBatchNumber || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '储位',
          align: 'center',
          dataIndex: 'storageLocation_dictText',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.storageLocation_dictText || '';
            const valB = b.storageLocation_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '入库日期',
          align: 'center',
          dataIndex: 'entryDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.entryDate || '';
            const valB = b.entryDate || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '库龄（天）',
          align: 'center',
          dataIndex: 'inventoryAge',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.inventoryAge;
            const valB = b.inventoryAge;
            if (valA == null && valB == null) return 0;
            if (valA == null) return 1;
            if (valB == null) return -1;
            return Number(valA) - Number(valB);
          },
        },
        {
          title: '质检状态',
          align: 'center',
          dataIndex: 'qualityInspectionStatus_dictText',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.qualityInspectionStatus_dictText || '';
            const valB = b.qualityInspectionStatus_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '供应商名称',
          align: 'center',
          dataIndex: 'supplierName',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.supplierName || '';
            const valB = b.supplierName || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '宝洁采购订单号',
          align: 'center',
          dataIndex: 'pgPurchaseOrderNumber',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.pgPurchaseOrderNumber || '';
            const valB = b.pgPurchaseOrderNumber || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '宝洁供应商编号',
          align: 'center',
          dataIndex: 'pgSupplierCode',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.pgSupplierCode || '';
            const valB = b.pgSupplierCode || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '宝洁供应商',
          align: 'center',
          dataIndex: 'pgSupplierName',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.pgSupplierName || '';
            const valB = b.pgSupplierName || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '客户名称',
          align: 'center',
          dataIndex: 'customerName',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.customerName || '';
            const valB = b.customerName || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '物料分类1',
          align: 'center',
          dataIndex: 'materialClassify_dictText',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.materialClassify_dictText || '';
            const valB = b.materialClassify_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '物料分类2',
          align: 'center',
          dataIndex: 'brand_dictText',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.brand_dictText || '';
            const valB = b.brand_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '更新时间',
          align: 'center',
          dataIndex: 'updateTime',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.updateTime || '';
            const valB = b.updateTime || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'creator_dictText',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.creator_dictText || '';
            const valB = b.creator_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '制单人部门',
          align: 'center',
          dataIndex: 'creatorDeptCode_dictText',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.creatorDeptCode_dictText || '';
            const valB = b.creatorDeptCode_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '制单日期',
          align: 'center',
          dataIndex: 'creationDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.creationDate || '';
            const valB = b.creationDate || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '经办人',
          align: 'center',
          dataIndex: 'handler_dictText',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.handler_dictText || '';
            const valB = b.handler_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '经办人部门',
          align: 'center',
          dataIndex: 'handlerDeptCode_dictText',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.handlerDeptCode_dictText || '';
            const valB = b.handlerDeptCode_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '经办日期',
          align: 'center',
          dataIndex: 'handlingDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.handlingDate || '';
            const valB = b.handlingDate || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '检验标准',
          align: 'center',
          dataIndex: 'inspectionStandard',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.inspectionStandard || '';
            const valB = b.inspectionStandard || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '抽检数量',
          align: 'center',
          dataIndex: 'samplingQuantity',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.samplingQuantity;
            const valB = b.samplingQuantity;
            if (valA == null && valB == null) return 0;
            if (valA == null) return 1;
            if (valB == null) return -1;
            return Number(valA) - Number(valB);
          },
        },
        {
          title: '检验结果',
          align: 'center',
          dataIndex: 'inspectionResult',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.inspectionResult || '';
            const valB = b.inspectionResult || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '检验人',
          align: 'center',
          dataIndex: 'inspector_dictText',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.inspector_dictText || '';
            const valB = b.inspector_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '检验部门',
          align: 'center',
          dataIndex: 'inspectionDeptCode_dictText',
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.inspectionDeptCode_dictText || '';
            const valB = b.inspectionDeptCode_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '检验日期',
          align: 'center',
          dataIndex: 'inspectionDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
          sorter: (a, b, order) => {
            if(a.id == '合计' && order == 'ascend') return 1;
            if(a.id == '合计' && order == 'descend') return -1;
            const valA = a.inspectionDate || '';
            const valB = b.inspectionDate || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark',
          scopedSlots: { customRender: 'remark' },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      settingColumns: [],

      defColumns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 40,
          align: 'center',
          fixed: 'left',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '单据类型',
          align: 'center',
          dataIndex: 'entryType_dictText',
          fixed: 'left',
        },
        {
          title: '单据号',
          align: 'center',
          dataIndex: 'documentNumber',
          fixed: 'left',
        },
        /*
        {
          title: '入库单号',
          align: 'center',
          dataIndex: 'entryNumber'
        },
        */
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'materialCode',
          fixed: 'left',
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName',
          fixed: 'left',
        },
        {
          title: '客户物料编码',
          align: 'center',
          dataIndex: 'customerMaterialCode',
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'specificationModel',
        },
        // {
        //   title: '物料描述',
        //   align: 'center',
        //   dataIndex: 'materialDesc',
        // },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText',
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'quantity',
        },
        {
          title: '辅单位',
          align: 'center',
          dataIndex: 'secondaryUnit_dictText',
        },
        {
          title: '辅数量',
          align: 'center',
          dataIndex: 'secondaryQuantity',
        },
        {
          title: '宝洁订单号',
          align: 'center',
          dataIndex: 'pgOrderNumber',
        },
        {
          title: '订单编号',
          align: 'center',
          dataIndex: 'orderNumber',
        },
        {
          title: '生产车间',
          align: 'center',
          dataIndex: 'productionShop_dictText',
        },
        {
          title: '生产日期',
          align: 'center',
          dataIndex: 'productionDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '保质期（天）',
          align: 'center',
          dataIndex: 'shelfLife',
        },
        {
          title: '限用日期',
          align: 'center',
          dataIndex: 'expirationDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '批次号',
          align: 'center',
          dataIndex: 'batchNumber',
        },
        {
          title: '物料批号',
          align: 'center',
          dataIndex: 'materialBatchNumber',
        },
        {
          title: '储位',
          align: 'center',
          dataIndex: 'storageLocation_dictText',
        },
        {
          title: '入库日期',
          align: 'center',
          dataIndex: 'entryDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '库龄（天）',
          align: 'center',
          dataIndex: 'inventoryAge',
        },
        {
          title: '质检状态',
          align: 'center',
          dataIndex: 'qualityInspectionStatus_dictText',
        },
        {
          title: '供应商名称',
          align: 'center',
          dataIndex: 'supplierName',
        },
        {
          title: '宝洁采购订单号',
          align: 'center',
          dataIndex: 'pgPurchaseOrderNumber',
        },
        {
          title: '宝洁供应商编号',
          align: 'center',
          dataIndex: 'pgSupplierCode',
        },
        {
          title: '宝洁供应商',
          align: 'center',
          dataIndex: 'pgSupplierName',
        },
        {
          title: '客户名称',
          align: 'center',
          dataIndex: 'customerName',
        },
        {
          title: '物料分类1',
          align: 'center',
          dataIndex: 'materialClassify_dictText',
        },
        {
          title: '物料分类2',
          align: 'center',
          dataIndex: 'brand_dictText',
        },
        {
          title: '更新时间',
          align: 'center',
          dataIndex: 'updateTime',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'creator_dictText',
        },
        {
          title: '制单人部门',
          align: 'center',
          dataIndex: 'creatorDeptCode_dictText',
        },
        {
          title: '制单日期',
          align: 'center',
          dataIndex: 'creationDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '经办人',
          align: 'center',
          dataIndex: 'handler_dictText',
        },
        {
          title: '经办人部门',
          align: 'center',
          dataIndex: 'handlerDeptCode_dictText',
        },
        {
          title: '经办日期',
          align: 'center',
          dataIndex: 'handlingDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '检验标准',
          align: 'center',
          dataIndex: 'inspectionStandard',
        },
        {
          title: '抽检数量',
          align: 'center',
          dataIndex: 'samplingQuantity',
        },
        {
          title: '检验结果',
          align: 'center',
          dataIndex: 'inspectionResult',
        },
        {
          title: '检验人',
          align: 'center',
          dataIndex: 'inspector_dictText',
        },
        {
          title: '检验部门',
          align: 'center',
          dataIndex: 'inspectionDeptCode_dictText',
        },
        {
          title: '检验日期',
          align: 'center',
          dataIndex: 'inspectionDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark',
          scopedSlots: { customRender: 'remark' },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/wms/inventory/pageListAndTotal',
        // list: '/wms/inventory/pageList',
        delete: '/wms/inventory/delete',
        deleteBatch: '/wms/inventory/deleteBatch',
        exportXlsUrl: '/wms/inventory/exportXls',
      },
      dictOptions: {},
      superFieldList: [],
      queryParam: {
        warehouseCode: 'W01',
      },
      ipagination: {
        current: 1,
        pageSize: 30,
        pageSizeOptions: ['30', '100', '300', '500', '1000'],
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
      },
      newDataSource: [],
      initPageSize: 30,
    }
  },
  watch: {
    'queryParam.warehouseCode': {
      handler(newVal) {
        if (newVal) {
          this.loadData()
        }
      },
      immediate: true,
    },
  },
  created() {
    this.initPageSize = this.ipagination.pageSize
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    loadData(arg){
      if (!this.url.list) {
        this.$message.error("请设置url.list属性!")
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      var params = this.getQueryParams();//查询条件
      this.loading = true;
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = Object.values(res.result.records)
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
        } else {
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.loading = false
      })
    },
    /*如果分页走这个方法*/
    dataHandling() {
      //动态新增每页条数
      this.ipagination.pageSize = this.initPageSize + 1
      var arrs = this.newDataSource
      if (arrs.length > 0) {
        let item = {}
        item.rowIndex = '合计'
        item.id = '合计'
        var quantity = 0
        var secondaryQuantity = 0
        for (let i = 0; i < arrs.length; i++) {
          quantity = utils.add(quantity, arrs[i].quantity);
          secondaryQuantity = utils.add(secondaryQuantity, arrs[i].secondaryQuantity);
        }
        item.quantity = quantity
        item.secondaryQuantity = secondaryQuantity
        this.newDataSource.push(item)
      }
      this.dataSource = Object.values(this.newDataSource)
    },
    // 分页改变时调用组件里的方法
    onShowSizeChange(current, pageSize) {
      this.ipagination.current = current
      this.ipagination.pageSize = pageSize
      //调整每页请求数值
      this.initPageSize = this.ipagination.pageSize
      this.loadData()
    },
    handleTableChange(pagination, filters, sorter) {
      //调整每页请求数值
      this.initPageSize = this.ipagination.pageSize
      this.loadData();
    },
    handlePackagingWork(record) {
      this.$refs.packagingWorkModal.add(record)
      this.$refs.packagingWorkModal.title = '包装作业'
      this.$refs.packagingWorkModal.disableSubmit = false
    },
    handlePackagingWorkRecord() {
      this.$refs.packagingWorkRecordModal.detail()
      this.$refs.packagingWorkRecordModal.title = '包装作业记录'
      this.$refs.packagingWorkRecordModal.disableSubmit = false
    },
    handleHold(record) {
      this.$refs.holdRecordModal.add(record)
      this.$refs.holdRecordModal.title = '扣货'
      this.$refs.holdRecordModal.disableSubmit = false
    },
    handleHoldRecord() {
      this.$refs.holdRecordModal.detail()
      this.$refs.holdRecordModal.title = '扣货记录'
      this.$refs.holdRecordModal.disableSubmit = true
    },
    handleMove(record) {
      this.$refs.storageLocationAdjustmentRecordModal.add(record)
      this.$refs.storageLocationAdjustmentRecordModal.title = '储位调整'
      this.$refs.storageLocationAdjustmentRecordModal.disableSubmit = false
    },
    handleMoveRecord() {
      this.$refs.storageLocationAdjustmentRecordModal.detail()
      this.$refs.storageLocationAdjustmentRecordModal.title = '储位调整记录'
      this.$refs.storageLocationAdjustmentRecordModal.disableSubmit = true
    },
    handleTransfer(record) {
      this.$refs.transferRecordModal.add(record)
      this.$refs.transferRecordModal.title = '调拨'
      this.$refs.transferRecordModal.disableSubmit = false
    },
    handleTransferRecord() {
      this.$refs.transferRecordModal.detail()
      this.$refs.transferRecordModal.title = '调拨记录'
      this.$refs.transferRecordModal.disableSubmit = true
    },
    // handlePrint() {
    //   if (!this.selectValidate()) {
    //     return
    //   }
    //   this.$refs.labelPrintModalForm.print(this.selectionRows[0])
    //   this.$refs.labelPrintModalForm.title = '标签打印'
    // },
    handlePrint() {
      if (!this.selectValidate()) {
        return
      }
      const { inboundRecordItemId } = this.selectionRows[0]
      this.generateLabelsAndPrint(inboundRecordItemId)
    },
    generateLabelsAndPrint(inboundRecordItemId) {
      if (!inboundRecordItemId) {
        return
      }
      getAction('/wms/inbound/inbound-record/generate-labels-by-record', {
        itemId: inboundRecordItemId,
      }).then((res) => {
        let { result } = res
        if (result && result.length > 0) {
          const ids = result.map((item) => item.id).join('\',\'')
          window.open(`${window._CONFIG['domianURL']}/jmreport/view/1099651769747447808?id=${ids}`, '_blank')
        }
      })
    },
    selectValidate() {
      if (!this.selectionRows || this.selectionRows.length === 0) {
        this.$message.warning('请先选中一条记录')
        return false
      }
      if (this.selectionRows.length > 1) {
        this.$message.error('最多只能选择一条记录')
        return false
      }
      return true
    },
    initDictConfig() {},
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
